package com.saver.modules.sys.controller;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.DateUtils;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/sys/log/login")
public class SysLoginLogController {

    @Autowired
    private JdbcTool jdbcTool;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> map) {
        String createDate = (String) map.get("createDate");
        String username = (String) map.get("username");
        String sql = "select * from sys_login_log t where 1=1 ";
        if (username != null && !"".equals(username)) {
            sql = sql + "and t.username=" + username;
        }
        if (createDate != null && !createDate.equals("")) {
            String[] split = createDate.split(",");
            Date start = new Date(Long.parseLong(split[0]));
            Date end = new Date(Long.parseLong(split[1]));
            String startFormat = DateUtils.format(start, DateUtils.DATE_TIME_PATTERN);
            String endFormat = DateUtils.format(end, DateUtils.DATE_TIME_PATTERN);
            sql = sql + " and (t.create_date >= '" + startFormat + "' and t.create_date <='" + endFormat + "')";
        }
        Object sidx = map.get("sidx");
        Object order = map.get("order");
        if ("".equals(sidx) && "".equals(order)) {
            map.put("sidx","t.create_date");
            map.put("order","desc");
        }
        PageUtils pageUtils = jdbcTool.queryPage(sql, new Query(map));
        return R.ok().put("page", pageUtils);
    }
}
